package com.yowasa.xiuxian.engine.message.user;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuXianExtend;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.RedisService;
import com.yowasa.xiuxian.service.helper.ExtendHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.PagescreenshotUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@CMD(name = "/破界", scope = CmdScopeEnum.ALL, outCmd = true)
public class MoveVillaHandler extends CmdMessageHandler {
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private RedisService redisService;


    @Override
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUser(message);
        String redisKey = "mys:xiuxian:userInfo:updateVilla:"+userBO.getUserId();
        if(redisService.hasKey(redisKey)){
            String i = (String) redisService.get(redisKey);
            throw new HandlerException("转区行为还在CD中,冷却结束时间："+i);
        }
        // 创建一个SimpleDateFormat对象，指定日期时间的格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        DateTime dateTime = DateUtil.offsetDay(now, 5);
        String dateStr = sdf.format(dateTime);
        redisService.set(redisKey,dateStr,60*60*24*5);
        userBO.setGuildId(message.getVilla_id());
        userHelper.saveUserInfo(userBO);
        messageHelper.replyMys(message,"你已成功转区至当前别野");
    }

    public String desc() {
        return new StringBuffer().append("修改自己的归属别野为当前发言别野，有5天的cd")
                .toString();

    }
}
